package com.wisdudu.blank.ui.login;

import android.databinding.ObservableField;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.CompoundButton;

import com.kelin.mvvmlight.base.ViewModel;
import com.kelin.mvvmlight.bindingadapter.edittext.ViewBindingAdapter;
import com.kelin.mvvmlight.command.ReplyCommand;
import com.wisdudu.blank.R;
import com.wisdudu.blank.data.Injection;
import com.wisdudu.blank.data.repo.UserRepo;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.Abs;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.ProgressSubscriber;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.SubscriberOnNextErrorListener;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.SubscriberOnNextListener;
import com.wisdudu.blank.databinding.FragmentUserRegisterBinding;
import com.wisdudu.blank.support.util.ToastUtil;
import com.wisdudu.blank.support.widget.loading.LoadingProgressDialog;
import com.wisdudu.blank.ui.setting.system.WebViewFragment;
import com.wisdudu.frame.support.util.RegularUtil;

/**
 * Created by ypp on 2016/8/23.
 */
public class RegisterVM implements ViewModel {
    private static final String TAG = "RegisterVM";
    private FragmentUserRegisterBinding mBingding;
    public ObservableField<String> phone = new ObservableField<>();
    private SubscriberOnNextListener<Abs> mEidtUserSubscriber;
    private RegisterFragment mFragment;
    private UserRepo mUserRepo;


    public RegisterVM(RegisterFragment registerFragment, FragmentUserRegisterBinding mBingding) {
        this.mFragment = registerFragment;
        this.mBingding = mBingding;
        mUserRepo = Injection.provideUserRepo();
        addListener();
    }

    private void addListener() {
        mBingding.cbUserprotocol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b == true && !TextUtils.isEmpty(phone.get()) && phone.get().length() == 11) {
                    mBingding.btnNext.setClickable(true);
                    mBingding.btnNext.setBackgroundResource(R.drawable.shape_rect_cor_blue);
                } else {
                    mBingding.btnNext.setClickable(false);
                    mBingding.btnNext.setBackgroundResource(R.drawable.shape_rect_cor_blue_unselected);
                }
            }
        });
    }

    public ReplyCommand step1OnClick = new ReplyCommand(() -> {
        mFragment.hideKeyboard();
        getSmsCode();
    });

    public ReplyCommand onUserprotocolClick = new ReplyCommand(() -> {
        String url = "http://www.wisdudu.com/clause/clause.html";
        entryWebViewActivity(url, "使用条款");
    });

    private void entryWebViewActivity(String url, String title) {
        Bundle bundle = new Bundle();
        bundle.putString(WebViewFragment.EXTRA_TITLE, title);
        bundle.putString(WebViewFragment.EXTRA_URL, url);
        WebViewFragment webViewFragment = new WebViewFragment();
        webViewFragment.setArguments(bundle);
        mFragment.addFragment(webViewFragment);
    }

    public ReplyCommand<ViewBindingAdapter.TextChangeDataWrapper> onTextChange = new ReplyCommand<ViewBindingAdapter.TextChangeDataWrapper>((wrapper) -> {
        if (!TextUtils.isEmpty(wrapper.s) && wrapper.s.length() == 11 && mBingding.cbUserprotocol.isChecked()) {
            mBingding.btnNext.setClickable(true);
            mBingding.btnNext.setBackgroundResource(R.drawable.shape_rect_cor_blue);
        } else {
            mBingding.btnNext.setClickable(false);
            mBingding.btnNext.setBackgroundResource(R.drawable.shape_rect_cor_blue_unselected);
        }
    });

    private void getSmsCode() {
        if (TextUtils.isEmpty(phone.get())) {
            ToastUtil.INSTANCE.toast("手机号码不能为空");
            return;
        }
        if (!RegularUtil.isMobile(phone.get())) {
            ToastUtil.INSTANCE.toast("手机号码格式错误");
            return;
        }
        mUserRepo.getSmsCode(phone.get(), 1).subscribe(new ProgressSubscriber<>(new SubscriberOnNextErrorListener<Object>() {

            @Override
            public void onError(Throwable e, LoadingProgressDialog pd) {
                ToastUtil.INSTANCE.toast(e.getMessage());
            }

            @Override
            public void onNext(Object code, LoadingProgressDialog pd) {
                pd.setTitle("发送成功");
                mFragment.addFragment(GetCodeFragment.newIntance(phone.get(), String.valueOf(code)));
            }

        }, mFragment.getActivity(), "正在发送...", 1000));
    }
}
